<?php

namespace App\Listeners;

use App\Events\SendNoticeToOne;
use App\Models\Deal;
use App\Models\Notification;
use App\Models\User;
use App\Models\RentCloth;
use App\Models\RentClothOrder;
use App\Models\ShopRole;

class SendMsgToOne
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param  SendNotice  $event
     * @return void
     */
    public function handle(SendNoticeToOne $event)
    {
        $data = $event->data;
        $type = $event->type;
        $msgType = $event->msgType;
        switch ($msgType) {
            case '1': //会员变动
                $icon = 0;
                $user_id = $data->id;
                $end_time = date('Y年m月d日', $data->member_end_time);
                if ($data->is_member) {
                    $title = '会员办理成功';
                    $content = "恭喜您已成功成为会员，您的会员卡截止日期为{$end_time}，我们将提供优质的服务。";
                } else {
                    $title = '会员已取消';
                    $content = '您的会员已取消，建议重新充值或续费，继续享受优质服务';
                }
                break;
            case '2': //角色竞选
                $icon = 0;                
                $user_id = $data->user_id;
                $role = $data->role;
                if ($role == 1) {
                    $role_name = '店长';
                    $card_num = 30;
                }
                if ($role == 2) {
                    $role_name = '店员';
                    $card_num = 30;
                }
                if ($role == 3) {
                    $role_name = '模特';
                    $card_num = 30;
                }
                $title = "竞选{$role_name}成功";
                $content = "恭喜您成功竞选为{$data->shop->shop_name}，您已经获得{$card_num}张会员卡，可在个人中心会员卡查看。";
                break;
            case '3':
                $icon = 0;                
                $user_id = $data->user_id;
                $title = "众筹项目成功";
                $content = "恭喜您的众筹项目已经成功，我们将会尽快与您取得联系。";
                break;
            case '4':
                $icon = 0;                
                $user_id = $data->user_id;
                $title = "众筹项目失败";
                $content = "抱歉，您发起的众筹项目失败，感谢您的支持，希望下次您能众筹成功。";
                break;
            case '5':
                $icon = 0;                
                $user_id = $data->user_id;
                $title = "点赞获得奖励";
                $content = "恭喜您，您的晒单赞数已经达到了50，您将获得 100算力的奖励，请注意查收。";
                break;
            case '6':
                $icon = 0;                
                $user_id = $data->id;
                $title = "邀请好友获得奖励";
                $content = "恭喜您，您成功邀请一个好友，您将获得10算力奖励，请注意查收。";
                break;
            case '7':
                $icon = 0;                
                $user_id = $data->user_id;
                $title = "充值成功";
                $content = "您已经充值成功，具体记录您可在个人中心查看详细记录。";
                break;
            case '8':
                $icon = 0;
                
                $user_id = $data->user_id;
                $title = "押金退还审核中";
                $content = "您的退还申请已经提交，我们会尽快审核，审核成功后将会退还给您。";
                break;
            case '9':
                $icon = 0;                
                $user_id = $data->id;
                $title = "押金退还成功";
                $content = "您的押金已经退还至您的余额，请在个人中心余额查看详细情况。";
                break;
            case '10':
                $icon = 0;                
                $user_id = $data->id;
                $title = "E格码挂单撤销成功";
                $content = "您的挂单记录已经撤销，E格码已经退还至您的账户，可到个人中心查看详情。";
                break;
            case '11':
                $icon = 0;
                $user_id = Deal::where('id', $data->deal_id)->value('user_id');
                $buyer = User::where('id', $data->user_id)->value('nickname');
                $count = $data->total_amount;
                $title = "有一笔交易成功";
                $content = "昵称为：{$buyer}的用户购买了{$count}个E格码，收益已放入您的余额，请注意查看。";
                break;
            case '12':
                $icon = 0;
                $user_id = Deal::where('id', $data->deal_id)->value('user_id');
                $buyer = User::where('id', $data->user_id)->value('nickname');
                $count = $data->total_amount;
                $title = "有一笔交易成功";
                $content = "昵称为：{$buyer}的用户购买了{$count}个E格码，收益已放入您的余额，请注意查看。";
                break;
            case '13':
                $user_id = RentClothOrder::where('id',$data->order_id)->value('user_id');   
                         
                $user = User::where('id', $data->user_id)->first();
                $icon = $user->avatar;
                $title = $user->nickname;
                $content = "你的晒单";
                break;
            case '14':
                $user_id = ShopRole::where('id',$data->role_id)->value('user_id');
                $user = User::where('id', $data->user_id)->first();
                $icon = $user->avatar;
                $title = $user->nickname;
                $content = "你的竞选";
                break;
            default:
                break;
        }

        $ins = [
            'icon' => $icon,
            'title' => $title,
            'content' => $content,
            'time' => date('Y-m-d H:i:s'),
        ];

        Notification::create([
            'user_id' => $user_id,
            'type' => $type,
            'data' => $ins,
            'create_id' => 0,
        ]);
    }
}
